package com.xyhuo.leetcode.december;

import java.util.HashMap;
import java.util.Map;

/**
 * 罗马数字转整数
 * https://leetcode.cn/problems/roman-to-integer/
 *
 * @time 2023/12/5
 */
public class RomanToInt {

//    public static void main(String[] args) {
//        RomanToInt romanToInt = new RomanToInt();
////        int i = romanToInt.romanToInt("MCMXCIV");
//        int i = romanToInt.romanToInt("III");
//        System.out.println(i);
//    }

    public int romanToInt(String s) {
        char[] roman = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        char[] charArray = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        map.put('I', 0);
        map.put('V', 1);
        map.put('X', 2);
        map.put('L', 3);
        map.put('C', 4);
        map.put('D', 5);
        map.put('M', 6);

        /**
         * I
         * II
         * III
         * IV
         * V
         * VI
         * VII
         * VIII
         * IX
         */

        int[] num = new int[4];
        int left = 0;
        int length = charArray.length;
        while (true) {
            if (left >= length) {
                break;
            }
            Integer leftIndex = map.get(charArray[left]);
            if (leftIndex % 2 == 0) {
                int count = 1;
                while (left + count < length) {
                    if (left + count >= length || charArray[left + count] != charArray[left]) {
                        break;
                    }
                    count++;
                }
                if (count == 1) {
                    int val = 1;
                    if (left + count < length && map.containsKey(charArray[left + count])) {
                        Integer rightIndex = map.get(charArray[left + count]);
                        if (leftIndex + 1 == rightIndex) {
                            val = 4;
                            left += 2;
                        } else if (leftIndex + 2 == rightIndex) {
                            val = 9;
                            left += 2;
                        } else {
                            left += 1;
                        }
                        num[leftIndex / 2] = val;
                    } else {
                        left += 1;
                        num[leftIndex / 2] = 1;
                    }
                } else {
                    left += count;
                    num[leftIndex / 2] = count;
                }
            } else {
                int tmp = roman[leftIndex - 1];
                int count = 1;
                while (left + count < length) {
                    if (left + count >= length || charArray[left + count] != tmp) {
                        break;
                    }
                    count++;
                }
                num[leftIndex / 2] = 5 + (count - 1);
                left += count;
            }
        }
        int ans = 0;
        for (int i = 0; i < 4; i++) {
            ans += Math.pow(10, i) * num[i];
        }
        return ans;
    }
}
